<?
  class add_contrl extends api{
    protected $path;
	protected $view;
    protected $model;
    protected $config;
    protected $db;
	protected $lang;
	protected $side;
    protected $url;
    protected $start_time;
    protected $template;
    private $module;

  // system_info && uri-------------------
    function analysis_url($base_url,$url){     $tmp=parse_url($base_url);
     if($tmp['path'] !="/"){
      $url=str_replace($tmp['path'],"",$url);
     }
     return  $url;
    }

    function direct_link($url){     if(count($url)==1){     $result=$this->model->GetInfoForLinkTreeAndModule($url[0]);
     if(empty($result)){     	return $url;
     	}else{     		$tmp=$url[0];
     	  $url[0]=$result['class'];
          $url[1]=$result['name'];
          $url[2]=$tmp;
          return $url;     		}
     }else{     return $url;
     }
    }

    function route($uri){     foreach($this->routes as $key=>$value){     $key = str_replace(':any', '.+', str_replace(':num', '[0-9]+', $key));
      if (preg_match('#^'.$key.'$#', $uri))
			{
              if (strpos($value, '$') !== FALSE AND strpos($key, '(') !== FALSE)
				{
					return preg_replace('#^'.$key.'$#', $value, $uri);

				}

			}
     }

     return  $uri;
    }



    function show_sys_info($url=""){
 	 if($url=="ok")return $this->view->view("templates/admin/sys_info/sys_info_ok.tpl","",true);
 	  else
 	   if( $url=="false")return $this->view->view("templates/admin/sys_info/sys_info_false.tpl","",true);

 	   return "";
    }
  // end SYstem_info---------------
  // time bechmark-----------      function start_time(){
	   $this->start_time=$this->get_time();
	  }

	  function end_time(){
		return $this->get_time()-$this->start_time;
	  }

	  function get_time(){
	   list($usec, $sec) = explode(" ",microtime());
       return ((float)$usec + (float)$sec);
	  }
  // end time-----------------

  // side control-------------
	  function return_side($tmp){
	  	foreach($tmp as $key=>$value){
	  	 $ret[$value['side']]=$value['free'];
	  	}
	   return $ret;
	  }

	  function contrl_side($data){
	  	if(is_array($data)){
       $keys=array_keys($data);
        foreach ($keys as $key=>$value){
         if (array_key_exists($value,$this->side)) $this->side[$value]=0;
        }
        }
	  }

	  function clean(){
       foreach($this->side as $key=>$value){
        $data[$key]="";
        }
        return $data;
       }
   // end side control----------


   // module function----------
      function save_mod_optoins($id,$class,$name,$ins,$help,$sch,$st,$tem,$even,$nl,$la){        foreach($_POST as $key=>$value){         if($key[0]=="*") $arr.=$_POST[$key];
        }
        $arr=explode(".",$arr);
        $this->model->save_mod_optoins($id,$class,$name,$ins,$help,$sch,$st,$tem,$even,$nl,$la,$arr);
      }


      function view_modules(){       $tmp=$this->model->load_cat_modules();
       foreach ($tmp as $key=>$value){
        if($value['schedule'])$value['m_sch']=$this->view->view("templates/admin/modules/tables/img_green.tpl","",true);
         else
          $value['m_sch']=$this->view->view("templates/admin/modules/tables/img_red.tpl","",true);

        if($value['standby'])$value['m_st']=$this->view->view("templates/admin/modules/tables/img_green.tpl","",true);
         else
          $value['m_st']=$this->view->view("templates/admin/modules/tables/img_red.tpl","",true);

         if($value['events'])$value['events']=$this->view->view("templates/admin/modules/tables/img_green.tpl","",true);
         else
          $value['events']=$this->view->view("templates/admin/modules/tables/img_red.tpl","",true);

        $ret.=$this->view->view("templates/admin/modules/tables/table.tpl",$value,true);
       }
       return $ret;
      }

      function return_info_model($id){      	$tmp=$this->model->return_info_model($id);       foreach($tmp as $key=>$value){

        if($value['schedule'])$value['opt_sch']=$this->view->view("templates/admin/modules/mod_options/mod_options_op_on.tpl","",true);
         else
          $value['opt_sch']=$this->view->view("templates/admin/modules/mod_options/mod_options_op_off.tpl","",true);
        $value['side_sch']=$this->comparison($value['class'],"schedule");
         if($value['standby'])$value['opt_stn']=$this->view->view("templates/admin/modules/mod_options/mod_options_op_on.tpl","",true);
         else
          $value['opt_stn']=$this->view->view("templates/admin/modules/mod_options/mod_options_op_off.tpl","",true);
        $value['side_st']=$this->comparison($value['class'],"standby");

        if($value['events'])$value['opt_event']=$this->view->view("templates/admin/modules/mod_options/mod_options_op_on.tpl","",true);
         else
          $value['opt_event']=$this->view->view("templates/admin/modules/mod_options/mod_options_op_off.tpl","",true);

	 }
         $value['all_mod']=$this->model->load_cat_modules("number_launch");
         $value['max_level']=$this->model->return_max_level();
	   return  $this->view->view("templates/admin/modules/mod_options/table_tr.tpl",$value,true);;
      }

      function comparison($class,$arg){

       $tmp_t=$this->model->return_all_templates();


	   foreach($tmp_t as $key=>$value){
        $ret.=$this->view->view("templates/admin/modules/mod_options/mod_options_tr_separatorSide.tpl",$value,true);

        $tmp_side=$this->model->return_side($value['id']);
        $tmp_sidMod=$this->model->return_side_module($class."_".$arg);
        foreach($tmp_side as $key1=>$value1){         foreach($tmp_sidMod as $key2 =>$value2){          if($value2['side']==$value1['side'])$value1['chechk']=" checked ";
         }         $value1['id']=$value['id'];
         $value1['name']=$arg;
         $value1['i']=$i++;
	     $ret.=$this->view->view("templates/admin/modules/mod_options/mod_options_tr_ch.tpl",$value1,true);
	    }


	   }

	   return $ret;
      }




     function instal_module($path){
     //exec( 'unzip -P ' . $password . ' ' . $fileZip . ' -d ' . $directory);.
     if(file_exists($path)){
      $xml=simplexml_load_file($path);
      chdir(SITE_DIR."modules/");
      mkdir(((string)$xml->module_info->name_class),0777);
      foreach($xml->module_file->folder as $m_f){
       chdir(SITE_DIR.$m_f['path']) ;
        mkdir( $m_f);
      }

      foreach($xml->module_file->file as $m_f){
       copy (SITE_DIR."modules/upload/".$m_f,SITE_DIR.$m_f['path'].$m_f);
       unlink(SITE_DIR."modules/upload/".$m_f);
      }
      unlink(SITE_DIR."modules/upload/instal.xml");
      $this->model->registry($xml->module_info->name_class,$xml->module_info->name_start_func,$xml->module_options->schedule,$xml->module_options->standby,$xml->module_options->side_schedule,$xml->module_options->side_standby,$xml->module_options->template_path,$xml->module_info->name_module,$xml->module_info->help->annotation);
      if ($xml->module_database->attributes()=="on")$this->model->create_table($xml,$xml->module_database->db_name);
     }

 	}


 // end module function--------



 // template func----------------------------
    function return_info_today_tpl(){     $data['id']=$this->template[0]['id'];
     $data['path_file']=$this->template[0]['path_file'];
     $data['name']=$this->template[0]['name'];
     $data['path_small_img']=$this->template[0]['path_small_img'];
     $data['show']=$this->view->view("templates/admin/modules/tables/img_green.tpl","",true);
     foreach($this->side as $key=>$value){      $td['side']=$key;      $tmp.=$this->view->view("templates/admin/templates/template_info_table_td.tpl",$td,true);
     }
     $data['td']=$tmp;
     return $this->view->view("templates/admin/templates/template_info_table.tpl",$data,true);
    }

    function return_side_contrl($id=0){    $temp=$this->model->return_side($id);     foreach($temp as $key=>$value){      if($value['free'])      $value['name_file']="unlock.jpg";
       else
        $value['name_file']="lock.jpg";
      $tmp.=$this->view->view("templates/admin/templates/template_side_tr.tpl",$value,true);
     }
     $data['tr']=$tmp;
     unset($tmp);
     $tmp=$this->model->return_all_templates();
     foreach($tmp as $key=>$value){      if($value['id']==$id )
       $ret.=$this->view->view("templates/admin/templates/template_side_tr2(2).tpl",$value,true);
        else
        $ret.=$this->view->view("templates/admin/templates/template_side_tr2.tpl",$value,true);
     }
     $data['id']=$temp[0] ['id_template'] ;
     $data['tr_tem']=$ret;
     return $this->view->view("templates/admin/templates/template_side.tpl",$data,true);
    }

	function return_templates_all(){	  $tmp=$this->model->return_all_templates();
	  foreach ($tmp as $key=>$value){       $data['id']=$value['id'];
       $data['path_file']=$value['path_file'];
       $data['name']=$value['name'];
       $data['path_small_img']=$value['path_small_img'];
       $data['show']=$value['show_me'];
       $data['show_n']=!$value['show_me'];
       $ret.=$this->view->view("templates/admin/templates/templates_all_tr.tpl",$data,true);	  }
	  $data['tr']=$ret;
     return $this->view->view("templates/admin/templates/templates_all.tpl",$data,true);
    }

    function template_load_del(){      $tmp=$this->model->return_all_templates();
      foreach ($tmp as $key=>$value){
       $data['name']=$value['name'];
       $data['name_cat']=$value['name_project'];
       $ret.=$this->view->view("templates/admin/templates/templates_l_d_tr.tpl",$data,true);
	  }
	  $data['tr']=$ret;

     return $this->view->view("templates/admin/templates/templates_l_d.tpl",$data,true);
    }

    function choice_template($url){       	if(isset($url[2])){         $id_template=$this->model->get_info($url[2],"template_out","","ns_tree","name");
         if(empty($id_template)){          $this->side=$this->return_side($this->model->return_side());
      	  $this->template=$this->model->return_info_template();
      	  return false;
          }else{
      	   $this->side=$this->return_side($this->model->return_side($id_template));
      	   $tmp_template=$this->model->get_info($id_template,"path_file","","templates");
      	   $this->template[0] ['id']=$id_template;
      	   $this->template[0] ['path_file']=$tmp_template;
      	   return true;
          }
         }else{          $this->side=$this->return_side($this->model->return_side());
      	  $this->template=$this->model->return_info_template();
         }

    }

     	function instal_template($path){
 	 //exec( 'unzip -P ' . $password . ' ' . $fileZip . ' -d ' . $directory);.
 	 if(file_exists($path)){
      $xml=simplexml_load_file($path);
      chdir(SITE_DIR."templates/site/tpl_site/");
      mkdir(((string)$xml->template_info->name_project),0777);
 	 }

 	 foreach($xml->template_file->folder as $t_f){
      chdir(SITE_DIR.$t_f['path']) ;
      mkdir( $t_f);
 	 }

 	 foreach($xml->template_file->file as $t_f){
   	  copy (SITE_DIR."templates/upload/".$t_f,SITE_DIR.$t_f['path'].$t_f);
      unlink(SITE_DIR."templates/upload/".$t_f);
 	 }
 	 unlink(SITE_DIR."templates/upload/".$xml->template_info->instal_file_name);
 	 $this->model->registry_template($xml->template_info->name_template,$xml->template_info->name_project,$xml->template_info->path_file,$xml->template_info->path_small_img,$xml->template_options->show_me);
 	 $id=$this->model->get_info($xml->template_info->name_project,"id","","templates","name_project");
 	 foreach($xml->template_options->side as $t_s){
 	  $this->model->save_side($t_s,$id);
 	 }
 	}
 // end template-----------------------------

 // security---------------------------------
   function return_user_security(){    $tmp=$this->model->return_user();
    foreach($tmp as $key=>$value){
     $ret.=$this->view->view("templates/admin/security/security_user_tr.tpl",$value,true);
    }
    $data['tr']=$ret;
    return $this->view->view("templates/admin/security/security_user.tpl",$data,true);
   }

   function return_element($oper){   	if (isset($oper)){     $tmp=$this->model->return_element(1);
       foreach($tmp as $key=>$value){
      if($value['id']==$oper)
       $ret.=$this->view->view("templates/admin/security/security_element_tr_2+.tpl",$value,true);
        else
         $ret.=$this->view->view("templates/admin/security/security_element_tr_2.tpl",$value,true);
      }
       if($oper){        $tmp=$this->model->return_element($oper);
        foreach($tmp as $key=>$value){
         $ret2.=$this->view->view("templates/admin/security/security_element_tr.tpl",$value,true);
        }
         $ret_temp['tr2']= $ret2;
         $ret2=$this->view->view("templates/admin/security/security_element_table_cat.tpl",$ret_temp,true);
         $ret=str_replace("{tr2}",$ret2,$ret);
        }
      $data['id']=$oper;
 	  $data['tr']=$ret;
      return $this->view->view("templates/admin/security/security_element_2.tpl",$data,true);
   	 }else{      $tmp=$this->model->return_element(0);
      foreach($tmp as $key=>$value){
      $ret.=$this->view->view("templates/admin/security/security_element_tr.tpl",$value,true);
      }
     $data['tr']=$ret;     return $this->view->view("templates/admin/security/security_element.tpl",$data,true);
   	 }
   }

   function level_access($url){    $t_level=$_SESSION['user']['level'];
    $f_level=$this->model->return_level_form($url);
    if ($t_level>=$f_level[0] ['level'] && isset($f_level [0] ['level'])) return true;
     else
      return false;
   }

   function edit_profile($id=""){   	if ($id==""){     $tmp=$this->model->return_user();
     foreach($tmp as $key=>$value){
      $ret.=$this->view->view("templates/admin/security/security_edit_user_tr.tpl",$value,true);
     }
     $tmp=$this->model->return_user_type();
     foreach($tmp as $key=>$value){
      $ret_s.=$this->view->view("templates/admin/security/security_edit_user_select.tpl",$value,true);
  	  $ret_l.=$this->view->view("templates/admin/security/security_edit_user_tr_l.tpl",$value,true);
     }
    $data['select_op']=$ret_s;
    $data['tr_u']=$ret;
     $data['tr_l']=$ret_l;
    return $this->view->view("templates/admin/security/security_edit_user.tpl",$data ,true);
     }else{     	$tmp=$this->model->return_user();
      foreach($tmp as $key=>$value){
       $data['id']=$value['id'];
       $data['name']=$value['name'];
       $data['adress']=$value['adress'];
       $data['pass']=$value['pass'];
       $data['type_a']=$value['type'];
      }

       $tmp=$this->model->return_user_type();
      foreach($tmp as $key=>$value){
       $data['type']=$value['type'];
       $ret_s.=$this->view->view("templates/admin/security/security_edit_user_select.tpl",$data,true);
       }
       $data['select_op']=$ret_s;
      return $this->view->view("templates/admin/security/security_edit_user_one.tpl",$data ,true);
     }
   }

   function edit_login($id){    $tmp=$this->model->return_user_type($id);
     foreach($tmp as $key=>$value){
      }    return $this->view->view("templates/admin/security/security_edit_login.tpl",$value ,true);
   }
 // end security-----------------------------

 //document---------------------------------------
   function document_pagination($pag,$link,$oper,$arg,$lang,$search){   	if(isset($_POST['search']))$pag=1;   	$pag_tmp=$pag;
    $pag=($pag*5)-5;
    $sm=SUM_PAGE;   	$tmp=$this->model->query_pagination($pag,$sm,$oper,$arg,$lang,$search);
   	$total=$this->model->get_info('',"FOUND_ROWS()","SELECT FOUND_ROWS();");
   	if(!empty($tmp)){
   	 foreach($tmp as $key=>$value){   	  $value['pag']=$pag_tmp;   	  $value['text']=substr($value['text'],0,40);   	  $ret.=$this->view->view("templates/admin/document/document_tr.tpl",$value,true);
   	 }

   	 $page= ceil(($total)/5);

         for($i=1;$i<=$page;$i++){
         $data['link']=$link.$i."/".$oper."/".$arg."/".$lang."/".$search;
         $data['num']=$i;
           if($i==$pag_tmp){
           	$ret_t['pagination'].=$this->view->view("templates/admin/table/pagination2.tpl",$data,true);
           	}else{
        	 $ret_t['pagination'].=$this->view->view("templates/admin/table/pagination.tpl",$data,true);
        	}
         }
     $ret.=$this->view->view("templates/admin/table/pagination_construct.tpl",$ret_t,true);
   	 return $ret;
   	 }else{
   	 $ret_t['pagination']=$this->lang['d_not_find'];
   	 return $this->view->view("templates/admin/table/pagination_construct.tpl",$ret_t,true);
     }
   }

   function document_all($pag,$link,$oper,$arg,$lang,$search){                $data['search']=$search;   				$data['doc_img_id']="";
       			$data['doc_img_dt']="";
       			$data['oper']=$oper;
                $data['vector2']=$arg;
                switch($arg){

                 case("up"):{
                  $data[$oper]=$this->view->view("templates/admin/document/document_img_up.tpl","",true);
                  $data['vector']="down";
                  if(isset($_POST['ch_lang']))$lang=$_POST['ch_lang'];
                  }break;

                 case("down"):{
              	  $data[$oper]=$this->view->view("templates/admin/document/document_img_down.tpl","",true);
              	  $data['vector']="up";
              	  if(isset($_POST['ch_lang']))$lang=$_POST['ch_lang'];

              	  }break;


                 default:{
                  $data['doc_img_id']="";
                  $data['doc_img_dt']="";
              	  $data['vector']="up";
                 }break;
              	}
      //if(empty($lang))$lang=ADMIN_LANG;
      $tmp=$this->_call_out_func("lang","get_lang");
      $val['name']=$lang;
      $data['option']=$this->view->view("templates/admin/document/document_option.tpl",$val,true);;
      foreach($tmp as $key=>$value){      	if($lang!=$value['name'])      	$data['option'].=$this->view->view("templates/admin/document/document_option.tpl",$value,true);
      } ;
      $data['lang']=$lang;
      $data['pag']=$pag;
      $data['tr']=$this->document_pagination($pag,$link,$oper,$arg,$lang,$search);
      return $this->view->view("templates/admin/document/document_table.tpl",$data,true);
   }

 //end document----------------------------------

 //launch id--------------------------------------
  function startLaunch($id,$lang=""){  				$data['lang']=$this->_call_out_func("lang","get_lang");
  			if(empty($lang))$lang=$_SESSION['lang'];  			$datas['freeLink']=$this->model->returnFreeLink();
  			foreach($datas['freeLink'] as $key=>$value){  			$data['freeLink'][]=$this->_call_out_func($value['module'],"_returnNLink",$value['id']);
  			}
  			$datas['all_doc']=$this->model->returnLink($id);
  			foreach($datas['all_doc'] as $key=>$value){            $data['all_doc'][]=$this->_call_out_func($value['module'],"_returnLink",$value['id'],$lang); // print_r($data['all_doc']);
  			}
  			//$data['all_doc']=$this->model->GetALLModulesForPage($id);
  			$data['cur_lang']=$lang;
  			$data['idc']=$id;
            $data['side_doc']=$this->model->return_side();
            $data['create_module']=$this->model->GetModelFroCreate();
            return $this->view->view("templates/admin/id/id.tpl",$data,true);
  }

 //end launch id----------------------------------

  }
?>